package bepviet.android.ui;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import bepviet.android.engine.DataFoodAdapter;
import bepviet.android.engine.R;
import bepviet.android.engine.handleImage;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddMonAn extends Activity {

	private static final int SELECT_PICTURE = 1;
	private String selectedImagePath;

	private DataFoodAdapter dataAdapter;
	private TextView clickback, tenmonan, gioithieu, nguyenlieu, thuchien,theloai;
	private Button btnAdd, btnImage;
	private ContentValues values;
	private LinearLayout layoutmain;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_food);
		
		clickback = (TextView) findViewById(R.id.txtback);
		btnAdd = (Button) findViewById(R.id.btnaddfood);
		btnImage = (Button) findViewById(R.id.btnimage);
		
		theloai = (TextView) findViewById(R.id.txttheloai);		
		tenmonan = (TextView) findViewById(R.id.txttenmonan);
		gioithieu = (TextView) findViewById(R.id.txtgioithieu);
		nguyenlieu = (TextView) findViewById(R.id.txtnguyenlieu);
		thuchien = (TextView) findViewById(R.id.txtthuchien);
		
		dataAdapter = new DataFoodAdapter(this);

		final Bundle bundle = getIntent().getExtras();
		btnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				 final SQLiteDatabase db = dataAdapter.OpenPath();
				 values = new ContentValues();				
				 values.put("TenMonAn", tenmonan.getText().toString());
				 values.put("LoaiMonAn", theloai.getText().toString());
				 values.put("GioiThieu", gioithieu.getText().toString());
				 values.put("NguyenLieuChuanBi", nguyenlieu.getText().toString());
				 values.put("ThucHien", thuchien.getText().toString());
				 values.put("HinhAnh",handleImage.toHex(selectedImagePath));
				 Long i = dataAdapter.Insert(db, values, "MonAn");
				 dataAdapter.Close(db);
				 if (i == 1)
				Toast.makeText(AddMonAn.this, selectedImagePath,
						Toast.LENGTH_LONG).show();

			}
		});

		clickback.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});

		btnImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
				photoPickerIntent.setType("image/*");
				startActivityForResult(photoPickerIntent, SELECT_PICTURE);   
//				 Intent intent = new Intent();
//                 intent.setType("image/*");
//                 intent.setAction(Intent.ACTION_GET_CONTENT);
//                 startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	

	

}
